package com.example.myapplication.ui.Home.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.example.myapplication.R;
import com.example.myapplication.base.ActivityManager;
import com.example.myapplication.base.BaseActivity;
import com.example.myapplication.model.bean.DataSynEvent;
import com.example.myapplication.model.bean.TabEntity;
import com.example.myapplication.ui.Home.fragments.DownloadFragment;
import com.example.myapplication.ui.Home.fragments.GrilFragment;
import com.example.myapplication.ui.Home.fragments.MainFragment;
import com.example.myapplication.ui.Home.fragments.MyFragment;
import com.example.myapplication.ui.Home.fragments.UpdateFragment;
import com.example.myapplication.ui.Home.fragments.VideoFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {
    private String[] mTitles = {"首页", "美女","视频","关注"};
    private int[] mIconUnselectIds = {
            R.mipmap.ic_home_normal,R.mipmap.ic_girl_normal,R.mipmap.ic_video_normal,R.mipmap.ic_care_normal};
    private int[] mIconSelectIds = {
            R.mipmap.ic_home_selected,R.mipmap.ic_girl_selected, R.mipmap.ic_video_selected,R.mipmap.ic_care_selected};

    private List<String> strings;
    private MainFragment mainFragment;
    private VideoFragment videoFragment;
    private GrilFragment grilFragment;
    private MyFragment myFragment;
    //更新
    private UpdateFragment updateFragment;
    //下载
    private DownloadFragment downloadFragment;

    @BindView(R.id.tablayout)
    CommonTabLayout tablayout;

    @BindView(R.id.navigationView)
    NavigationView navigationView;

    @BindView(R.id.drawerlayout)
    DrawerLayout drawerlayout;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    private static int tabLayoutHeight;

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        ActivityManager.addActivity(this);
        initTab();
        index(0);
        ViewTreeObserver vto2 = tablayout.getViewTreeObserver();
        vto2.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                tablayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                tabLayoutHeight=tablayout.getMeasuredHeight();
            }
        });
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        finish();
    }



    private  void  index(int position){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        hideFragments(fragmentTransaction);
        //  mViewHolderTitle.mTitleTextView.setText(strings.get(position));
        switch (position){
            case 0:
                if (mainFragment==null){
                    mainFragment=new MainFragment();
                    fragmentTransaction.add(R.id.base_activity_content,mainFragment);

                }else {
                    fragmentTransaction.show(mainFragment);
                }

                break;
            case 1:
                if (grilFragment==null){
                    grilFragment=new GrilFragment();
                    fragmentTransaction.add(R.id.base_activity_content,grilFragment);

                }else {
                    fragmentTransaction.show(grilFragment);
                }
                break;
            case 2:
                if (videoFragment==null){
                    videoFragment=new VideoFragment();
                    fragmentTransaction.add(R.id.base_activity_content,videoFragment);

                }else {
                    fragmentTransaction.show(videoFragment);
                }

                break;
            case 3:
                if (myFragment==null){
                    myFragment=new MyFragment();
                    fragmentTransaction.add(R.id.base_activity_content,myFragment);

                }else {
                    fragmentTransaction.show(myFragment);
                }

                break;
            case 4:
                if (updateFragment==null){
                    updateFragment=new UpdateFragment();
                    fragmentTransaction.add(R.id.base_activity_content,updateFragment);

                }else {
                    fragmentTransaction.show(updateFragment);
                }

                break;
            case 5:
                if (downloadFragment==null){
                    downloadFragment=new DownloadFragment();
                    fragmentTransaction.add(R.id.base_activity_content,downloadFragment);

                }else {
                    fragmentTransaction.show(downloadFragment);
                }

                break;
            case 6:
                break;
        }
        fragmentTransaction.commit();
    }
    /**
     * 隐藏fragment
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mainFragment != null) {
            transaction.hide(mainFragment);
        }
        if (grilFragment != null) {
            transaction.hide(grilFragment);
        }
        if (videoFragment != null) {
            transaction.hide(videoFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
        if (updateFragment != null) {
            transaction.hide(updateFragment);
        }
        if (downloadFragment != null) {
            transaction.hide(downloadFragment);
        }
    }

    /**
     * 初始化tab
     */
    private void initTab() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        tablayout.setTabData(mTabEntities);
        //点击监听
        tablayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
              index(position);
           }
            @Override
            public void onTabReselect(int position) {
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 普通事件
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDataSynEvent(DataSynEvent event) {
        startAnimation(event.isfend);
    }
    /**
     * 菜单显示隐藏动画
     * @param showOrHide
     */
    private void startAnimation(boolean showOrHide){
        final ViewGroup.LayoutParams layoutParams = tablayout.getLayoutParams();
        ValueAnimator valueAnimator;
        ObjectAnimator alpha;
        if(showOrHide==true){
            valueAnimator = ValueAnimator.ofInt(tabLayoutHeight, 0);
            alpha = ObjectAnimator.ofFloat(tablayout, "alpha", 1, 0);
        }else{
            valueAnimator = ValueAnimator.ofInt(0, tabLayoutHeight);
            alpha = ObjectAnimator.ofFloat(tablayout, "alpha", 0, 1);
        }
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                layoutParams.height= (int) valueAnimator.getAnimatedValue();
                tablayout.setLayoutParams(layoutParams);
            }
        });
        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.setDuration(500);
        animatorSet.playTogether(valueAnimator,alpha);
        animatorSet.start();
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        item.setChecked(true);
        drawerlayout.closeDrawers();
        String title = item.getTitle().toString();
        if (title.equals("更新")){
            index(4);
        }else if  (title.equals("下载")){
            index(5);
        }else {
            index(6);
        }
        return true;
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (NiceVideoPlayerManager.instance().onBackPressd()) {
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
